﻿/* Path 扩展类
 */

/* 未完事项
 * 1、单元测试。
 */
namespace LSLib.IO
{
    using Microsoft.Win32;
    using System;
    using System.IO;
    using System.Collections.Generic;

    public class PathEx
    {
        /// <summary>获取系统特殊文件夹的目录路径
        /// </summary>
        public static string GetSpecialFolder(Environment.SpecialFolder item)
        {
            return Environment.GetFolderPath(item);
        }

        /// <summary>合并多个路径字符串。
        /// 针对.net2.0版本添加次扩展方法。
        /// </summary>
        public static string CombineMultiPath(params string[] pathes)
        {
            if (pathes.Length == 0) return string.Empty;

            string result = string.Empty;
            foreach (string next in pathes)
            {
                result = Path.Combine(result, next);
            }
            return result;
        }

        /// <summary>常量值：iexplore.exe
        /// </summary>
        public const string NAME_IExplore = "iexplore.exe";

        /// <summary>获取IE浏览器的路径
        /// </summary>
        public static string GetPath_IEBrowser()
        {
            string programFilesPath = GetSpecialFolder(Environment.SpecialFolder.ProgramFiles);
            return Path.Combine(programFilesPath, "Internet Explorer", NAME_IExplore);
        }

        /// <summary>获取默认浏览器的路径
        /// </summary>
        public static string GetPath_DefaultWebBrowser()
        {
            string result = string.Empty;
            string subName = Path.Combine("http", "shell", "open", "command");

            using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(subName))
            {
                if (key.GetValue(string.Empty) == null) return string.Empty;

                result = key.GetValue(string.Empty).ToString();
                if (result.Contains("\""))
                {
                    result = result.TrimStart('"');
                    result = result.Substring(0, result.IndexOf('"'));
                }
            }
            return result;
        }

        /// <summary>检测该字符是否可用于路径名中
        /// </summary>
        /// <param name="pathChar">路径名中的字符</param>
        /// <returns>可用：true；不可用：false。</returns>
        public static bool IsUseablePathChar(char pathChar)
        {
            List<char> list = new List<char>(Path.GetInvalidPathChars());
            return list.Contains(pathChar) == false;
        }
    }
}